package zxj.spay.modular.user.service.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import zxj.spay.base.Result;
import zxj.spay.base.resource.SessionName;
import zxj.spay.entity.User;
import zxj.spay.modular.user.ctrl.UserCtrl;
import zxj.spay.modular.user.dao.UserDao;
import zxj.spay.modular.user.service.UserService;
import zxj.spay.util.IPUtil;
import zxj.spay.util.SHA2Util;

@Service
public class UserServiceImpl implements UserService{
	@Resource
	private UserDao userDao;
	
	@Override
	public Result login(User user, HttpServletRequest request) {
		user.setPassword(SHA2Util.sha256(user.getPassword()));
		User dbuser = userDao.findUserByUsernameAndPassword(user.getUsername(),user.getPassword());
		if(null == dbuser){
			String msg = "用户名或者密码错误";
			UserCtrl.LOG.info(msg);
			return new Result(Result.WARN,msg);
		}else{
			UserCtrl.LOG.info("用户登陆成功，登陆IP["+IPUtil.getIpAddr(request)+"],用户:" + dbuser);
			request.getSession().setAttribute(SessionName.USER, dbuser);
			return new Result(Result.SUCCESS);
		}
	}

	
	public static void main(String[] args) {
		String str = "123456";
		String sha256 = SHA2Util.sha256(str);
		System.out.println(sha256);
	}
}
